package com.itheima;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import org.junit.Test;

/**
 * @author 张一恒
 * @version 1.0
 * @description:
 * @date 2022/12/16 10:33
 */
public class JacobDemo {

    @Test
    public void test(){
        String source = "D:\\员工(李四)合同.docx";
        String target = "D:\\员工(李四)合同.pdf";
        System.out.println("Word转PDF开始启动...");

        ActiveXComponent app =null;
        try {
            //调用window中的程序
            app = new ActiveXComponent("Word.Application");
            //调用的时候不显示窗口
            app.setProperty("Visible",false);
            //获得所有打开的文档
            Dispatch docs = app.getProperty("Documents").toDispatch();
            //打开指定的文件
            Dispatch doc = Dispatch.call(docs, "Open", source).toDispatch();

            //另存为，将文档保存为pdf，其中word保存为pdf的格式 宏的值是17
            //调用保存功能
            Dispatch.call(doc,"SaveAs",target,17);
            //保存完关闭word文档
            Dispatch.call(doc,"Close");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭office
            if(app!=null){
                app.invoke("Quit",0);
            }
        }

    }
}
